<?php 
class cogClassAction extends ActionManage
{
	public function initAction()
	{
		$this->keyss = $this->option->getval('systemnum');
		if(isempt($this->keyss)){
			$rnd  	= md5(str_shuffle('abcedfghijk').rand(1000,9999));
			$this->option->setval('systemnum', $rnd);
			$this->keyss = $rnd;
		}
	}
	
	public function defaultAction()
	{
		$type = $this->get('type');
		$this->assign('type', $type);
	}
	
	public function getxings($str)
	{
		if(!isempt($str))$str = substr($str,0,5).'*****'.substr($str,-5);
		return $str;
	}
	
	public function chushihAction()
	{
		if(getconfig('systype')=='demo')return '演示不要改';
		$deltem = explode(',','log,logintoken,worc,word');
		return '暂无此功能，可以去到数据库删除操作';
	}
	
	public function savecheckAction()
	{
		if(getconfig('systype')=='demo')return '演示不要改';
		$type = $this->post('type');
		$msg  = 'ok';
		if(isempt($type))$msg = $this->saveconfig('title,xinhukey,apptheme,rockfile_url,rockfile_key');
		if($type=='server')$msg  = $this->saveconfig('server_push,phppath');
		
		return $msg;
	}
	
	private function saveconfig($cont)
	{
		$path = ''.P.'/'.P.'Config.php';
		
		$neir = file_get_contents($path);
		$zdar = explode(',', $cont);
		$neira= explode("\n", $neir);
		
		$strs = '';
		$szida= array();
		foreach($neira as $line){
			if($line==');')break;
			$bo = false;
			foreach($zdar as $fid){
				if(contain($line,"'".$fid."'")){
					$val = $this->post($fid);
					if(contain($val,'*****')){
						$strs.="".$line."\n";
					}else{
						$strs.="	'".$fid."'	=> '".$val."',\n";
					}
					$bo = true;
					$szida[]=$fid;
				}
			}
			if(!$bo)$strs.="".$line."\n";
		}
		
		foreach($zdar as $fid){
			if(!in_array($fid, $szida)){
				$val = $this->post($fid);
				$strs.="	'".$fid."'	=> '".$val."',\n";
			}
		}
		
		$strs.=');';
		$bo = @file_put_contents($path,$strs);
		if(!$bo)return '无权限写入：'.$path.'';
		return 'ok';
	}

	public function textpushAction()
	{
		$rand = rand(10000,99999);
		$barr = c('reim')->push('cli,test',array(
			'rand' => $rand
		));
		if(!$barr['success'])return $barr;
		sleep(3);
		$new = c('cache')->get('pushtest');
		if($new!=$rand)return returnerror('推送地址不能用');
		
		return returnsuccess('推送地址可以用');
	}
	
	/**
	*	检查是否更新
	*/
	public function gengxinAction()
	{
		return c('xinhuapi')->getdata('gengxin','check', array(
			'nowver'=>VERSION
		));
	}
	
	/**
	*	获取文件更新
	*/
	public function okgengkAction()
	{
		$barr = c('xinhuapi')->getdata('gengxin','file');
		if(!$barr['success'])return $barr;
		$path = $barr['data']['path'];
		$wenjf= ''.UPDIR.'/logs/rockupage_'.time().'.zip';
		$this->rock->createtxt($wenjf, file_get_contents($path));
		if(!file_exists($wenjf))return returnerror('目录'.UPDIR.'/logs无法写入，无法更新');
		
		$farr 	= c('zip')->zipget($wenjf);
		if(!is_array($farr))return returnerror('无法更新1');
		
		$usshu = 0;
		$filestr = '本次更新文件：';
		foreach($farr as $k=>$rs){
			$_pluj = $rs['filepath'];
			$upbo  = true;
			if(file_exists($_pluj)){
				$size = filesize($_pluj);
				if($size == $rs['filesize'])$upbo = false;
			}
			if(contain($_pluj,'version.php'))$upbo = true;
			if($upbo){
				$bo = $this->rock->createtxt($_pluj, base64_decode($rs['filecontent']));
				if(!$bo)return returnerror('无法写入:'.$_pluj.'');
				$usshu++;
				$filestr.='<br>'.$usshu.'、'.$_pluj.'';
			}
		}
		
		$sarr = returnsuccess('更新'.$usshu.'个文件，请刷新');
		$sarr['filestr'] = $filestr;
		return $sarr;
	}
}